<?php
/* Plugin Extra feeds - http://www.osclass.org/ */

function divendo_cars() {
    
    header("Content-type: text/xml"); 
    echo '<?xml version="1.0" encoding="utf-8"?>
            <divendo>';

    if(osc_count_items()) {
        while(osc_has_items()) {
            $item = feed_get_car_data(osc_item());

            $date = date('d/m/Y');
            $time = date('H:i');

            if(preg_match('|([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})|', osc_item_pub_date(), $tmp)) {
                $date = $tmp[3]."/".$tmp[2]."/".$tmp[1];
                $time = $tmp[4].":".$tmp[5].":00";
            }
            
            echo '<ad>
                    <id><![CDATA['.osc_item_id().']]></id>
                    <url><![CDATA['.osc_item_url().']]></url>
                    <title><![CDATA['.osc_item_title().']]></title>
                    <content><![CDATA['.get_item_desc($item).']]></content>';                    
                    if(osc_item_price() > 0)
                       echo '<price><![CDATA['.get_item_price_xml($item).']]></price>';                    
                    echo '<city><![CDATA['.((osc_item_city()!='')?osc_item_city():'').']]></city>                   
                    <region><![CDATA['.((osc_item_region()!='')?osc_item_region():'').']]></region>    
                    <make><![CDATA['.((isset($item['s_make']))?$item['s_make']:'').']]></make>
                    <model><![CDATA['.((isset($item['s_descaux']))?$item['s_descaux']:'').']]></model>   
                    <color><![CDATA['.((isset($item['s_color']))?$item['s_color']:'').']]></color>
                    <year><![CDATA['.((isset($item['i_registration']))?$item['i_registration']:'').']]></year>                    
                    <mileage><![CDATA['.((isset($item['i_mileage']))?$item['i_mileage']:'').']]></mileage>
                    <doors><![CDATA['.((isset($item['i_doors']))?$item['i_doors']:'').']]></doors>
                    <seats><![CDATA['.((isset($item['i_seats']))?$item['i_seats']:'').']]></seats>
                    <fuel><![CDATA['.((isset($item['e_fuel']))?$item['e_fuel']:'').']]></fuel>
                    <transmission><![CDATA['.((isset($item['e_suspension']))?$item['e_suspension']:'').']]></transmission>';
                    
            $res_string = '';
            if(osc_count_item_resources()>0) {
                while(osc_has_item_resources()) {
                    if(strpos(osc_resource_type(), 'image')!==FALSE) {
                        $res_string .= '<picture>
                                            <picture_url><![CDATA['.osc_resource_url().']]></picture_url>
                                            <picture_title><![CDATA['.osc_item_title().']]></picture_title>
                                        </picture>';
                    }
                }
            }
            if($res_string!='') {
                echo '<pictures>'.$res_string.'</pictures>';
            }                
            echo '<date><![CDATA['.$date.']]></date>
                <time><![CDATA['. $time.']]></time>
            </ad>';
        }
    }    
    echo '</divendo>';
}

?>
